package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.converter.ProductDetailConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Period;
import java.util.*;

/**
 * 创建日期: 2022/05/19 13:30
 *
 * @author yangfan
 */
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ProductDetailConverter productDetailConverter;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();

        try {
            // 参数校验
            request.requestCheck();
            // 查询商品
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            // 转化
            ProductDetailDto productDetailDto = productDetailConverter.item2DetailDto(item);
            productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setDetail(itemDesc.getItemDesc());

            // 放入数据
            productDetailResponse.setProductDetailDto(productDetailDto);
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse,e);
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {

        AllProductResponse allProductResponse = new AllProductResponse();

        try {
            // 参数检验
            request.requestCheck();
            // 查询商品
            String sort = request.getSort();
            List<Item> items;
            if ("".equals(sort) || sort == null) {
                // 综合排序
                items = itemMapper.selectItemFront(request.getCid(), "id", "DESC", request.getPriceGt(), request.getPriceLte());
            } else if (sort.equals("1")) {
                items = itemMapper.selectItemFront(request.getCid(), "price", "ASC", request.getPriceGt(), request.getPriceLte());
            } else {
                items = itemMapper.selectItemFront(request.getCid(), "price", "DESC", request.getPriceGt(), request.getPriceLte());
            }

            // 转换
            List<ProductDto> productDtos = productConverter.items2Dto(items);
            Integer page = request.getPage();
            Integer size = request.getSize();
            List<ProductDto> productDtosByPage;
            if ( ( page * size ) > productDtos.size() ) {
                productDtosByPage = productDtos.subList((page - 1) * size, productDtos.size());
            } else {
                productDtosByPage = productDtos.subList( ( page - 1 ) * size, page * size);
            }

            // 放入数据
            allProductResponse.setProductDtoList(productDtosByPage);
            allProductResponse.setTotal(((long) productDtos.size()));
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse,e);
        }

        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        RecommendResponse recommendResponse = new RecommendResponse();

        try {
            List<Panel> panels = panelMapper.selectPanelContentById(6);
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);

            // 转化数据
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            PanelDto panelDto = contentConverter.panel2Dto(panels.get(0));
            panelDto.setPanelContentItems(panelContentItemDtos);

            Set<PanelDto> panelDtos = new LinkedHashSet<>();
            panelDtos.add(panelDto);
            // 存储数据
            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse,e);
        }

        return recommendResponse;
    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}
